package com.zhuchaooasystem.presenter.home.fragment.oa.sign;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.SignListEntity;
import com.zhuchaooasystem.entity.SignNumEntity;
import com.zhuchaooasystem.entity.UserInfo;

import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class SignPresenter extends BasePresenter<SignContract.View> implements SignContract.Presenter {

    public SignPresenter(Activity activity, SignContract.View view) {
        super(activity, view);
    }

    public UserInfo getLoginInfo() {
        UserInfo user = UserInfo.getInstance();
        return user;
    }

    public int getUserId() {
        int userId = UserInfo.getInstance().getMemberId();
        return userId;
    }


    @Override
    public void getSignNum() {
        final int userId = getUserId();

        Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("PersonnelPK", userId + "");
        Subscription subscription = HttpFactory.getHttpApi().getPunchCardsInfo(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            SignNumEntity entity = (SignNumEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.setSignNum(entity.getResult().getSuccessful());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void pagerChanged(int position) {
        mView.setCurrentItem(position);
        mView.radioGroupChanged(position);
    }


    @Override
    public void getSignRecord(final int index) {
        final int userId = getUserId();
        Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("PersonnelPK", userId + "");
        map.put("PageIndex", index + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().attHisToryList(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        SignListEntity entity = (SignListEntity) o;
                        if (null != o) {
                            if (null != entity.getResult().getSuccessful()) {
                                if (index == 1) {
                                    mView.refreshData(entity.getResult().getSuccessful().getListdata());
                                } else {
                                    mView.loadMoreData(entity.getResult().getSuccessful().getListdata());
                                }
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);

    }
}
